<?php
/**
*
*	后台友情链接管理 @zairwolf
*/
if(!defined('IN_ACP')) exit('Access Denied');


//添加
if($action=='add') {
	$sitename=_post('sitename');
	$sitelogo=_post('sitelogo');
	$siteurl=_post('siteurl');
	if(!$sitename || !$siteurl) b('站名为空');
		$linkid = db_i("insert into rd8_link set name='$sitename',url='$siteurl'");
		if($sitelogo) {
			rfow(ROOT.'data/logo/'.$linkid.'.'.rfileext($sitelogo), file_get_contents('http://'.$sitelogo));
			$sitelogo = 'data/logo/'.$linkid.'.'.rfileext($sitelogo);

			db_q("update rd8_link set logo='$sitelogo' where id='$linkid'");
		}
	j("?ptype=$ptype&pfile=$pfile", '修改成功');
}

//删除
if($action=='delete') {
	$linkid=_get('linkid');
	if(!$linkid) b('无ID');
	db_q("DELETE FROM rd8_link WHERE id='$linkid'");
	j("?ptype=$ptype&pfile=$pfile", '删除成功');
}

//审核
if($action=='verify') {
	$linkid=_get('linkid');
	if(!$linkid) b('无ID');
	db_q("UPDATE rd8_link SET apply='0' WHERE id='$linkid'");
	j("?ptype=$ptype&pfile=$pfile", '审核成功');
}

//移动
if($action=='move') {
	$linkid=_get('linkid');
	$move=_get('move');
	if(!$linkid) b('无ID');
	if(!$move) b('无移动');
	$rows = db_rows("select id, priority from rd8_link where apply=0 order by priority ASC");
	foreach($rows as $k => $v) {
		db_q("update rd8_link set priority='".($k+1)."' where id='$v[id]'");
		if($v['id'] == $linkid) $p = $k + 1;
	}
	if($move > 0) {
		db_q("update rd8_link set priority='$p' where priority='".($p+1)."'");
		db_q("update rd8_link set priority='".($p+1)."' where id='$linkid'");
	}else {
		db_q("update rd8_link set priority='$p' where priority='".($p-1)."'");
		db_q("update rd8_link set priority='".($p-1)."' where id='$linkid'");
	}
	j("?ptype=$ptype&pfile=$pfile", '移动成功');
}

//默认显示
if(!$action){
	$tpl = new T($pfile);
	$tpl->assign(array(
		'links'	=>	db_rows("select * from rd8_link where apply=0 order by priority ASC"),
		'apply'	=>	db_rows("select * from rd8_link where apply=1 order by priority ASC"),
	));
	$tpl->output();
}
